﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using SEXY;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

namespace Studio
{
	// Token: 0x0200051E RID: 1310
	public class SceneLoadScene : MonoBehaviour
	{
		// Token: 0x06002153 RID: 8531 RVA: 0x000CC2C0 File Offset: 0x000CA4C0
		public void OnClickThumbnail(int _id)
		{
			this.canvasWork.enabled = true;
			this.select = 12 * SceneLoadScene.page + _id;
			this.imageThumbnail.texture = this.buttonThumbnail[_id].texture;
		}

		// Token: 0x06002154 RID: 8532 RVA: 0x000CC2F8 File Offset: 0x000CA4F8
		private void OnClickClose()
		{
			Singleton<Scene>.Instance.UnLoad();
		}

		// Token: 0x06002155 RID: 8533 RVA: 0x000CC308 File Offset: 0x000CA508
		private void OnClickPage(int _page)
		{
			this.SetPage(_page);
		}

		// Token: 0x06002156 RID: 8534 RVA: 0x000CC314 File Offset: 0x000CA514
		private void OnClickLoad()
		{
			Singleton<Studio>.Instance.LoadScene(this.listPath[this.select]);
			this.canvasWork.enabled = false;
			base.StartCoroutine("NotificationLoadCoroutine");
		}

		// Token: 0x06002157 RID: 8535 RVA: 0x000CC34C File Offset: 0x000CA54C
		private IEnumerator NotificationLoadCoroutine()
		{
			yield return null;
			NotificationScene.spriteMessage = this.spriteLoad;
			NotificationScene.waitTime = 1f;
			Singleton<Scene>.Instance.Load(new Scene.Data
			{
				sceneName = "StudioNotification",
				isLoading = false,
				isAsync = false,
				isFade = false,
				isOverlap = true,
				isAdd = true
			});
			yield break;
		}

		// Token: 0x06002158 RID: 8536 RVA: 0x000CC368 File Offset: 0x000CA568
		private void OnClickImport()
		{
			Singleton<Studio>.Instance.ImportScene(this.listPath[this.select]);
			this.canvasWork.enabled = false;
			base.StartCoroutine("NotificationImportCoroutine");
		}

		// Token: 0x06002159 RID: 8537 RVA: 0x000CC3A0 File Offset: 0x000CA5A0
		private IEnumerator NotificationImportCoroutine()
		{
			yield return null;
			NotificationScene.spriteMessage = this.spriteImport;
			NotificationScene.waitTime = 1f;
			Singleton<Scene>.Instance.Load(new Scene.Data
			{
				sceneName = "StudioNotification",
				isLoading = false,
				isAsync = false,
				isFade = false,
				isOverlap = true,
				isAdd = true
			});
			yield break;
		}

		// Token: 0x0600215A RID: 8538 RVA: 0x000CC3BC File Offset: 0x000CA5BC
		private void OnClickCancel()
		{
			this.canvasWork.enabled = false;
		}

		// Token: 0x0600215B RID: 8539 RVA: 0x000CC3CC File Offset: 0x000CA5CC
		private void OnClickDelete()
		{
			CheckScene.sprite = this.spriteDelete;
			CheckScene.unityActionYes = new UnityAction(this.OnSelectDeleteYes);
			CheckScene.unityActionNo = new UnityAction(this.OnSelectDeleteNo);
			Singleton<Scene>.Instance.Load(new Scene.Data
			{
				sceneName = "StudioCheck",
				isLoading = false,
				isAsync = false,
				isFade = false,
				isOverlap = false,
				isAdd = true
			});
		}

		// Token: 0x0600215C RID: 8540 RVA: 0x000CC448 File Offset: 0x000CA648
		private void OnSelectDeleteYes()
		{
			Singleton<Scene>.Instance.UnLoad();
			File.Delete(this.listPath[this.select]);
			this.canvasWork.enabled = false;
			this.InitInfo();
			this.SetPage(SceneLoadScene.page);
		}

		// Token: 0x0600215D RID: 8541 RVA: 0x000CC488 File Offset: 0x000CA688
		private void OnSelectDeleteNo()
		{
			Singleton<Scene>.Instance.UnLoad();
		}

		// Token: 0x0600215E RID: 8542 RVA: 0x000CC498 File Offset: 0x000CA698
		private void InitInfo()
		{
			for (int i = 0; i < this.transformRoot.childCount; i++)
			{
				UnityEngine.Object.Destroy(this.transformRoot.GetChild(i).gameObject);
			}
			this.transformRoot.DetachChildren();
			List<KeyValuePair<DateTime, string>> list = (from s in Directory.GetFiles(UserData.Create("studio/scene"), "*.png")
			select new KeyValuePair<DateTime, string>(File.GetLastWriteTime(s), s)).ToList<KeyValuePair<DateTime, string>>();
			CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
			Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("ja-JP");
			list.Sort((KeyValuePair<DateTime, string> a, KeyValuePair<DateTime, string> b) => b.Key.CompareTo(a.Key));
			Thread.CurrentThread.CurrentCulture = currentCulture;
			this.listPath = (from v in list
			select v.Value).ToList<string>();
			this.thumbnailNum = this.listPath.Count;
			this.pageNum = this.thumbnailNum / 12 + ((this.thumbnailNum % 12 == 0) ? 0 : 1);
			this.dicPage.Clear();
			for (int j = 0; j < this.pageNum; j++)
			{
				GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(this.prefabButton);
				gameObject.transform.SetParent(this.transformRoot, false);
				StudioNode component = gameObject.GetComponent<StudioNode>();
				component.active = true;
				int page = j;
				component.addOnClick = delegate()
				{
					this.OnClickPage(page);
				};
				component.text = string.Format("{0}", j + 1);
				this.dicPage.Add(j, component);
			}
		}

		// Token: 0x0600215F RID: 8543 RVA: 0x000CC674 File Offset: 0x000CA874
		private void SetPage(int _page)
		{
			StudioNode studioNode = null;
			if (this.dicPage.TryGetValue(SceneLoadScene.page, out studioNode))
			{
				studioNode.select = false;
			}
			_page = Mathf.Clamp(_page, 0, this.pageNum - 1);
			int num = 12 * _page;
			for (int i = 0; i < 12; i++)
			{
				int num2 = num + i;
				if (!MathfEx.RangeEqualOn<int>(0, num2, this.thumbnailNum - 1))
				{
					this.buttonThumbnail[i].interactable = false;
				}
				else
				{
					this.buttonThumbnail[i].texture = PngAssist.LoadTexture(this.listPath[num2]);
					this.buttonThumbnail[i].interactable = true;
				}
			}
			SceneLoadScene.page = _page;
			if (this.dicPage.TryGetValue(SceneLoadScene.page, out studioNode))
			{
				studioNode.select = true;
			}
			Resources.UnloadUnusedAssets();
			GC.Collect();
		}

		// Token: 0x06002160 RID: 8544 RVA: 0x000CC750 File Offset: 0x000CA950
		private void Awake()
		{
			this.InitInfo();
			this.SetPage(SceneLoadScene.page);
			this.buttonClose.onClick.AddListener(new UnityAction(this.OnClickClose));
			this.buttonLoad.onClick.AddListener(new UnityAction(this.OnClickLoad));
			this.buttonImport.onClick.AddListener(new UnityAction(this.OnClickImport));
			this.buttonCancel.onClick.AddListener(new UnityAction(this.OnClickCancel));
			this.buttonDelete.onClick.AddListener(new UnityAction(this.OnClickDelete));
			this.canvasWork.enabled = false;
		}

		// Token: 0x040022BF RID: 8895
		public static int page;

		// Token: 0x040022C0 RID: 8896
		[SerializeField]
		private ThumbnailNode[] buttonThumbnail;

		// Token: 0x040022C1 RID: 8897
		[SerializeField]
		private Button buttonClose;

		// Token: 0x040022C2 RID: 8898
		[SerializeField]
		private Canvas canvasWork;

		// Token: 0x040022C3 RID: 8899
		[SerializeField]
		private RawImage imageThumbnail;

		// Token: 0x040022C4 RID: 8900
		[SerializeField]
		private Button buttonLoad;

		// Token: 0x040022C5 RID: 8901
		[SerializeField]
		private Sprite spriteLoad;

		// Token: 0x040022C6 RID: 8902
		[SerializeField]
		private Button buttonImport;

		// Token: 0x040022C7 RID: 8903
		[SerializeField]
		private Sprite spriteImport;

		// Token: 0x040022C8 RID: 8904
		[SerializeField]
		private Button buttonCancel;

		// Token: 0x040022C9 RID: 8905
		[SerializeField]
		private Button buttonDelete;

		// Token: 0x040022CA RID: 8906
		[SerializeField]
		private Sprite spriteDelete;

		// Token: 0x040022CB RID: 8907
		[SerializeField]
		private Transform transformRoot;

		// Token: 0x040022CC RID: 8908
		[SerializeField]
		private GameObject prefabButton;

		// Token: 0x040022CD RID: 8909
		private List<string> listPath;

		// Token: 0x040022CE RID: 8910
		private int thumbnailNum = -1;

		// Token: 0x040022CF RID: 8911
		private Dictionary<int, StudioNode> dicPage = new Dictionary<int, StudioNode>();

		// Token: 0x040022D0 RID: 8912
		private int pageNum = -1;

		// Token: 0x040022D1 RID: 8913
		private int select = -1;
	}
}
